﻿<cfsilent>
	<cfscript>
		
		event.setArg("pageTitle", "学籍信息维护 - 学生专业方向分流");
		
		propAdvice = getProperty("serviceFactory").getBean("studentPropertyAdvice");
		
		/* 选择有方向的专业 */
		
		sql = "SELECT 
					a.sbj_id, a.sbj_name 
					FROM t_subject a 
				WHERE 
					EXISTS ( 
						SELECT 1 
						FROM t_subject b 
						WHERE 
							(b.sbj_direction > '0' OR b.sbj_direction < '0') 
							AND 
							SUBSTR(b.sbj_id,1,5) = a.sbj_id 
						) 
				ORDER BY a.sbj_name ";
		queryObj = new Query( datasource=application.dnsSlave) ;
		rs_subject = queryObj.execute( sql=sql ).getResult();	
		
		gradeArray = [];
		classArray = [];
		studentArray = [];
		subjectId = event.getArg("SubjectID");
		grade = event.getArg("Grade");
		classId = event.getArg("ClassID");
		
		rs_class = queryNew("sbj_id");
		rs_student = queryNew("sbj_id");

		if ( rs_subject.recordCount ) {

			/* 所有需设置方向的专业和年级的组合 */
			
			sql = "SELECT 
						distinct d.grade, 
						d.sbj_id, d.cls_id, d.cls_name
						FROM t_student a 
	   						 INNER JOIN t_class d ON d.cls_id = a.cls_id 
						WHERE 
							EXISTS (
	 						    SELECT 1
								FROM t_student_status c 
								WHERE 
									c.stu_id = a.stu_id 
									AND 
									SUBSTR(c.student_prop,1,1) = '1' 
							)
							AND 
							EXISTS (
	 						    SELECT 1
								FROM t_subject b 
								WHERE 
									(b.sbj_direction > '0' OR b.sbj_direction < '0') 
									AND
									SUBSTR(b.sbj_id,1,5) = d.sbj_id
							)
						ORDER BY d.grade DESC ";
			queryObj = new Query( datasource=application.dnsSlave) ;
			
			rs_class = queryObj.execute( sql=sql ).getResult();	
			
			if (rs_class.recordCount) {

				for (a=1; a LTE rs_class.recordCount; a++) {
					if ( not arrayFind(gradeArray, rs_class["grade"][a]) ) { arrayAppend(gradeArray, rs_class["grade"][a]); }
				}

				/* 设置专业和年级的默认值 */
				if ( subjectId eq "" ) { subjectId = rs_class["sbj_id"][1]; }
				if ( grade eq "" ) { subjectId = rs_class["grade"][1]; }
			
			}
			
			if ( len(subjectId) ) {

				/* 读取指定专业的下设方向 */
				
				sql = "SELECT 
							a.sbj_direction, a.sbj_short 
						FROM t_subject a  
						WHERE a.sbj_id LIKE :condition 
                              AND 
                              a.sbj_direction <> '0'";
				queryObj = new Query( datasource=application.dnsSlave) ;
				queryObj.addParam( name="condition", value=subjectId & "_", cfsqltype="cf_sql_varchar" );
				rs_dir = queryObj.execute( sql=sql ).getResult();	
				
			}
			
			if ( len(classId) ) {
				
				/* 读取可设置方向的学生 */
			
				sql = "SELECT 
						  a.stu_id, a.stu_name, 
                          a.stu_sex,d.sbj_name, 
                          c.grade, a.sbj_direction, 
						  b.student_prop 
						FROM t_student a 
						   INNER JOIN t_student_status b ON b.stu_id = a.stu_id  
						   INNER JOIN t_class c ON c.cls_id = a.cls_id 
						   INNER JOIN t_subject d ON d.sbj_id = c.sbj_id 
						WHERE 
							a.cls_id= :classId 
						ORDER BY a.stu_id ";
				queryObj = new Query( datasource=application.dnsSlave) ;
				queryObj.addParam( name="classId", value=classId, cfsqltype="cf_sql_char" );
				rs_student = queryObj.execute( sql=sql ).getResult();										
			}

		}
		
		
		currentTab = event.getArg("TabID", "labDetail");
		
		targetArgs = structNew();
		
	</cfscript>
</cfsilent>

<div id="headArea">
	<div class="uiHeader uiHeaderWithImage ptm">
		<div class="clearfix uiHeaderTop">
			<div class="uiHeaderActions rfloat">
				<a class="uiButton" href="<cfoutput>#buildURL('studentProfile', targetArgs)#</cfoutput>"><i class="mrs img btnList"></i><span class="uiButtonText">学籍档案</span></a>
			</div>
			<div>
				<h2 class="uiHeaderTitle">
					<i class="uiHeaderImage img icon16x16 application"></i>设置学生专业方向
				</h2>
			</div>
		</div>
	</div>
</div>

<div class="UIElement">
	<ul class="senateFirstPage">
		<li>
			<div class="clearfix" id="summary">
				<div class="UItab">
					<div class="tabLables">
						<span <cfif currentTab eq "labDetail">class="active"</cfif> id="labDetail" tabTarget="Detail">选择来源学生</span>
					</div>
				</div>
				<script language="javascript" type="text/javascript">
					//<![CDATA[
					<!--
					$(document).ready(function() {
						
						/* init Tab */
						$("div.tabContent").hide();
						$("div#" + $("div.tabLables span.active").attr("tabTarget")).show();
						
						$("div.tabLables > span").click(function(){
							var $targetID = $(this).attr("tabTarget");
							
							$("div.tabLables span").removeClass("active");
							$(this).addClass("active");
							
							$("div.tabContent").not('[id="' + $targetID + '"]').hide();
							$("div.tabContent#" + $targetID).show();
						});
						
					});
					// -->
					//]]>
				</script>
				
				<div id="Detail" class="tabContent">
					
					<cfif rs_class.recordCount>
					
						<form id="formFilter" class="formWrapper Transparent" method="post" action="<cfoutput>#buildURL('studentSubjectDirection')#</cfoutput>">
							来源专业：<select id="forSubject" name="SubjectID">
                                <cfloop query="rs_subject" >
									<option <cfif subjectId eq rs_subject.sbj_id>selected="selected"</cfif> value="<cfoutput>#rs_subject.sbj_id#</cfoutput>"><cfoutput>#rs_subject.sbj_name#</cfoutput></option>
								</cfloop>
							</select>
							来源年级：<select id="forGrade" name="Grade">
								<cfloop array="#gradeArray#" index="gid">
									<option <cfif grade eq gid>selected="selected"</cfif> value="<cfoutput>#gid#</cfoutput>"><cfoutput>#gid#</cfoutput></option>
								</cfloop>
							</select>
							行政班：<select id="forClass" name="ClassID"></select>
							
							<input class="button3" type="submit" value="查找学生" />
						</form>
						<cfset row = 0 />
						<script language="javascript" type="text/javascript">
							//<![CDATA[
							<!--
							$(document).ready(function() {
							
								/* class = [grade, subjectId, classId, className] */
								var classArray = [<cfloop query = "rs_class" >
								<cfset row++ />['<cfoutput>#rs_class.grade#</cfoutput>','<cfoutput>#rs_class.sbj_id#</cfoutput>','<cfoutput>#rs_class.cls_id#</cfoutput>','<cfoutput>#rs_class.cls_name#</cfoutput>']<cfif row lt rs_class.recordCount>,</cfif></cfloop>];
							
								function flushClass() {

									var subjectId = $("#forSubject").val();
									var grade = $("#forGrade").val();
								
									$("#forClass").removeOption(/./);
								
									for (var i = 0; i < classArray.length; i++) {
										if ( grade == classArray[i][0] && subjectId == classArray[i][1] ) {
											$("#forClass").addOption( classArray[i][2], classArray[i][3] );
										}
									}

									$("#forClass").val($("#forClass option:first").attr('value'));									
								}
							
								$("#forGrade").change(function(){ flushClass(); });
								$("#forSubject").change(function(){ flushClass(); });
								
								flushClass();
								
								<cfif len(classId)>
									$("#forClass").val('<cfoutput>#classId#</cfoutput>');
								</cfif>
							
							});
							// -->
							//]]>
						</script>
					</cfif>
					
					<cfif rs_student.recordCount>

						<cfset row = 0 />
						<table id="studentList" class="UIEditable">
							<thead>
								<tr>
									<td width="18"></td>
									<td width="90">学号</td>
									<td>姓名</td>
									<td width="25">性别</td>
									<td width="30">年级</td>
									<td>主修专业</td>
									<td>专业方向</td>
									<td width="25">在校</td>
									<td width="18"></td>
								</tr>
							</thead>
							<tbody>
                                <cfloop query="rs_student" >
									<cfset row++ />
									<cfset propAdvice.parseProperty(rs_student.student_prop) />
									<tr class="editRows <cfif not propAdvice.getInSchool()>alertRow</cfif>">
										<td class="index"><cfoutput>#row#</cfoutput></td>
										<td><span class="numeric"><cfoutput>#rs_student.stu_id#</cfoutput></span></td>
										<td><cfoutput>#rs_student.stu_name#</cfoutput></td>
										<td><cfif rs_student.stu_sex eq "1">男<cfelse>女</cfif></td>
										<td><span class="numeric"><cfoutput>#rs_student.grade#</cfoutput></span></td>
										<td><cfoutput>#rs_student.sbj_name#</cfoutput></td>
										<td nowrap="nowrap" sid="<cfoutput>#rs_student.stu_id#</cfoutput>">
												<a subdir="0" href="javascript:signDir('<cfoutput>#rs_student.stu_id#</cfoutput>','0');" class="box <cfif rs_student.sbj_direction eq '0'>checked</cfif>">无</a>
                                            <cfloop query="rs_dir" >
												<a subdir="<cfoutput>#rs_dir.sbj_direction#</cfoutput>" href="javascript:signDir('<cfoutput>#rs_student.stu_id#</cfoutput>','<cfoutput>#rs_dir.sbj_direction#</cfoutput>');" class="box <cfif rs_student.sbj_direction eq rs_dir.sbj_direction>checked</cfif>"><cfoutput>#rs_dir.sbj_short#</cfoutput></a>
											</cfloop>
										</td>
										<td><cfif propAdvice.getInSchool()>是<cfelse><span class="alertCell">否</span></cfif></td>
										<td></td>
									</tr>
								</cfloop>
							</tbody>
						</table>
						<div id="out"></div>
						<script language="javascript" type="text/javascript">
							//<![CDATA[
							<!--
							function signDir(studentId, dirId) {
								
								var $td = $("td[sid='" + studentId + "']", $("#studentList"));

								$.post(
										"<cfoutput>#buildURL('apiSignStudentDirection')#</cfoutput>",
										{
											'StuID' : studentId,
											'SubDir' : dirId,
											'seed': (new Date()).getTime()
										},
										function(data) {
											
											//$("#out").html(data);
											
											result = $.parseJSON(data);
											
											if ( result.success ) {

												/* 清除专业方向标识 */
												$("a.box", $td).removeClass("checked");
												
												var dir = result.SubjectDirection;
												
												$("a[subdir='" + dir + "']", $td).addClass("checked");
											}
											
										});
							}
							// -->
							//]]>
						</script>
							
						
					</cfif>
					
				</div>
			</div>
		</li>
	</ul>
</div>
